package com.itheima.reggie.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.reggie.util.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //元对象处理器
    @Override
    public void insertFill(MetaObject metaObject) {
        ThreadLocal<Object> objectThreadLocal = new ThreadLocal<>();
        Object o = objectThreadLocal.get();
        Long userId = (Long) BaseContext.get();

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser",userId);
        metaObject.setValue("updateUser", userId);
        long id = Thread.currentThread().getId();
        System.out.println("元对象处理器id = " + id);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long o = (Long) BaseContext.get();
        long id = Thread.currentThread().getId();
        Long userId = (Long) BaseContext.get();
        System.out.println("元对象处理器id = " + id);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", userId);
    }
}